import json
import pymysql
from twisted.enterprise import adbapi
from .settings import MYSQL_SETTINGS
import logging

class MysqlTwistedPipline(object):
    def __init__(self, dbpool):

        self.dbpool = dbpool
        print("__init__(self, dbpool)")

    @classmethod
    def from_settings(cls, settings):
        dbparms = dict(
            host=MYSQL_SETTINGS['HOST'],
            db=MYSQL_SETTINGS['DATABASE'],
            user=MYSQL_SETTINGS['USER'],
            passwd=MYSQL_SETTINGS['PASSWORD'],
            charset=MYSQL_SETTINGS['CHARSET'],
            cursorclass=pymysql.cursors.DictCursor,  # 指定 curosr 类型
            use_unicode=True,
        )
        # 指定擦做数据库的模块名和数据库参数参数
        print(dbparms)
        print(type(cls))
        dbpool = adbapi.ConnectionPool("pymysql", **dbparms)
        return cls(dbpool)

    def process_item(self, item, spider):
        '''
        使用twisted将mysql插入变成异步处理
        :param item:
        :param spider:
        :return:
        '''

        print(f"MYsql:{item}")
        query = self.dbpool.runInteraction(self.db_insert, item)
        # 指定异常处理方法
        query.addErrback(self.handle_error, item, spider)  # 处理异常
        return item

    def handle_error(self, failure, item, spider):
        # 处理异步插入的异常
        print("######################1")
        # print(item)
        # print(failure)
        #logging.error(f"handler_error has error failure: {failure}")
        #logging.warning()
        print(f"handler_error has error failure: {failure}")
        print("########################2")

    def db_insert(self, cursor, item):
        # 执行具体的插入
        # 根据不同的item 构建不同的sql语句并插入到mysql中
        print("-----------db_insert1------------------")
        insert_sql, params = self.get_insert_sql(item)

        cursor.execute(insert_sql, params)
        print("-----------db_insert2------------------")
        logging.info(f"write db ok with data:{item['name']}")


    def get_insert_sql(self, item):
        insert_sql = """
                INSERT INTO jd_goods(goods_title,
                                    goods_price,
                                    goods_image_url,
                                    goods_detail_url,
                                    goods_image_path) 
                                    VALUES (%s,%s,%s,%s,%s)
        """



        params = (
            item["goods_title"],
            item["goods_price"],
            item["goods_image_url"],
            item["goods_detail_url"],
            item["goods_image_path"])
        return insert_sql, params
